/**
 * @file jisp.h
 * @brief  ISP类，通过ISP协议访问单片机信息
 * 
 * Copyright (C) 2014 JakeYang <add358@gmail.com>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
#ifndef JISP_H
#define JISP_H

#include <QSerialPort>

class Jisp : public QSerialPort
{
    Q_OBJECT
public:
    explicit Jisp(QObject *parent = 0):QSerialPort(parent) {}

    void setFlashFile(const QString &flashFile) { m_flashFile = flashFile; }
    const QString& flashFile(void) const { return m_flashFile; }

    void flash();    //开始执行操作
    bool get_command(QString &msg);
    bool get_version(QString &msg);
    bool get_id(QString &msg);
    bool read_memory(int addr, int len, QByteArray &byteArray);

    enum ExecFlags {
        EXEC_ERROR,
        AUTO_BAUD,
        GET_COMMAND,
        GET_VERSION
    };

signals:
    void send_exec_msg(int execFlag, const QString & msg);
    void send_flash_progress(int value);

public slots:

private:
    QString m_flashFile;

    bool auto_baud(void);

    bool send_cmd(char cmd);
    bool send_addr(int addr);
    bool send_number(char num);
    void read_data(QByteArray &byteArray);
    char checksum(const QByteArray &byteArray);
    void display(const char *buf, int len);
};

#endif // JISP_H
